/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actioncombination.logic.agent;


import actioncombination.problem.gridworld.AgentStateImpl;
import actioncombination.common.value.DoubleValue;
import actioncombination.common.value.Value;
import actioncombination.logic.agent.knowledge.Knowledge;
import actioncombination.logic.environment.RLEnvironment;
import actioncombination.logic.rlmethod.RLMethodStrategy;
import java.util.Collection;



/**
 *
 * @author Administrador
 */
public abstract class RLAgent extends GoalBasedAgent {

    protected Knowledge knowledge;
    protected RLMethodStrategy method;

    public RLAgent() {
        //instancia el estado inicial del Agente
        this.setAgentState(new AgentStateImpl());        
    }    

    //Actualiza el valor del estado 
    public void update(AgentState oldState, AgentState newState, Value learningStep,
            Value reward, Value discountedRate) {
        //Value oldStateValue = this.getKnowledge().get(oldState.getId());
        //Value newStateValue = this.getKnowledge().get(newState);
        Double update = ((DoubleValue) oldState.getValue()).getValue() + ((DoubleValue) learningStep.getValue()).getValue() * (((DoubleValue) reward.getValue()).getValue() + ((DoubleValue) discountedRate.getValue()).getValue() * ((DoubleValue) newState.getValue()).getValue() - ((DoubleValue) oldState.getValue()).getValue());
        oldState.setValue(new DoubleValue(update));
        this.getKnowledge().update(oldState);
    }

    //Actualiza el valor del par estado-accion
//    public void update(AgentState oldStateAction, AgentState newStateAction, Value learningStep, Value reward, Value discountedRate) {
//        //Value oldStateValue = this.getKnowledge().get(oldStateAction);
//        //Value newStateValue = this.getKnowledge().get(newStateAction);
//        Double update = ((DoubleValue) oldStateAction.getValue()).getValue() + ((DoubleValue) learningStep.getValue()).getValue() * (((DoubleValue) reward.getValue()).getValue() + ((DoubleValue) discountedRate.getValue()).getValue() * ((DoubleValue) newStateAction.getValue()).getValue() - ((DoubleValue) oldStateAction.getValue()).getValue());
//        oldStateAction.setValue(new DoubleValue(update));
//        this.getKnowledge().update(oldStateAction);
//    }

    public RLMethodStrategy getMethod() {
        return method;
    }

    public void setMethod(RLMethodStrategy method) {
        this.method = method;
    }

    public Knowledge getKnowledge() {
        return knowledge;
    }

    public void setKnowledge(Knowledge knowledge) {
        this.knowledge = knowledge;
    }

    public Collection<AgentStateImpl> getNextPosiblesActionsStates() {
        return ((RLEnvironment) this.getEnvironment()).getNextPosiblesActionsStates((AgentStateImpl) this.getAgentState());
    }

    public void BeginEpisode() {
        this.getMethod().beginEpisode(this);
    }
}
